#include "Adetector.h"

bool GetFinalState_detector_make_signal_jet(CDraw &para, AObject &object){
	para.debug.Message(5,26,"in make signal jet: begin");
		
	int tagjet_max=0;
	if(para.jet.bjet.SWITCH==true){
		para.debug.Message(5,26,"fill bjets in object.Vsignal_tagjet");
		if(para.flow.exclusive){
			tagjet_max=para.signal.NUM_bjet;
		}
		else{
			tagjet_max=object.Vbjet.size();
		}
		for(int i=0;i<tagjet_max;i++){
			object.Vsignal_tagjet.push_back(object.Vbjet[i]);
		}
	}
	if(para.jet.cjet.SWITCH==true){
		para.debug.Message(5,26,"fill cjets in object.Vsignal_tagjet");
		if(para.flow.exclusive){
			tagjet_max=para.signal.NUM_cjet;
		}
		else{
			tagjet_max=object.Vcjet.size();
		}
		for(int i=0;i<tagjet_max;i++){
			object.Vsignal_tagjet.push_back(object.Vcjet[i]);
		}
	}
	if(para.jet.taujet.SWITCH==true){
		para.debug.Message(5,26,"fill taujets in object.Vsignal_tagjet");
		if(para.flow.exclusive){
			tagjet_max=para.signal.NUM_taujet;
		}
		else{
			tagjet_max=object.Vtaujet.size();
		}
		for(int i=0;i<tagjet_max;i++){
			object.Vsignal_tagjet.push_back(object.Vtaujet[i]);
		}
	}
	if(para.jet.qjet.SWITCH==true){
		para.debug.Message(5,26,"fill light jets in object.Vsignal_tagjet");
		if(para.flow.exclusive){
			tagjet_max=para.signal.NUM_qjet;
		}
		else{
			tagjet_max=object.Vqjet.size();
		}
		for(int i=0;i<tagjet_max;i++){
			object.Vsignal_tagjet.push_back(object.Vqjet[i]);
		}
	}
	if(para.jet.untagjet.SWITCH==true && para.jet.bjet.SWITCH==false  && para.jet.bjet.SWITCH==false  && para.jet.cjet.SWITCH==false  && para.jet.taujet.SWITCH==false  && para.jet.qjet.SWITCH==false  ){
		para.debug.Message(5,26,"only fill untagged jets in object.Vsignal_tagjet");
		if(para.flow.exclusive){
			tagjet_max=para.signal.NUM_untagjet;
		}
		else{
			tagjet_max=object.Vjet.size();
		}
		for(int i=0;i<tagjet_max;i++){
			object.Vsignal_tagjet.push_back(object.Vjet[i]);
		}
	}
	int min_needed_jet=para.signal.NUM_jets-object.Vsignal_tagjet.size();
	if(min_needed_jet>0){
		para.debug.Message(5,26,"above tagged jet number is smaller than what needed. fill other light jets in object.Vsignal_tagjet",para.signal.NUM_jets,object.Vsignal_tagjet.size());
		if(object.Vqjet.size()<min_needed_jet){
			return(false);
		}
		if(!para.flow.exclusive){
		    para.debug.Message(5,26,"the max tagjet will equal Vqjet size",object.Vqjet.size());
			tagjet_max=object.Vqjet.size();
		}
		else{
			tagjet_max=object.Vuntagjet.size();
		}
		para.debug.Message(5,26,"add qjet into tag jet",para.signal.NUM_qjet,object.Vqjet.size(),tagjet_max);
		for(int i=para.signal.NUM_qjet;i<tagjet_max;i++){
			object.Vsignal_tagjet.push_back(object.Vqjet[i]);
		}
	}
	for(int i=0;i<object.Vsignal_tagjet.size();i++){
		para.debug.Message(5,26,"fill Vsignal_tagjet in Lsignal_tagjet", object.Vsignal_tagjet.size(),i);
		object.Lsignal_tagjet.push_back(object.Vsignal_tagjet[i]->P4());
	}
	para.debug.Message(5,26,"sort Vsignal_tagjet, Lsignal_tagjet");
	std::sort(object.Lsignal_tagjet.begin(),object.Lsignal_tagjet.end(),TLV_compare_Pt);
	std::sort(object.Vsignal_tagjet.begin(),object.Vsignal_tagjet.end(),DelphesJet_compare_PT);
	return(true);

}
bool GetFinalState_detector_make_signal_lep(CDraw &para, AObject &object){
	int taglep_max, taglep_m_max, taglep_p_max;
	if(para.lep.SWITCH==true){
		if(para.flow.exclusive){
			taglep_max=para.signal.NUM_lep;
		}
		else{
			taglep_max=object.Llep.size();
		}
		for(int i=0;i<taglep_max;i++){
			object.Lsignal_lep.push_back(object.Llep[i]);
		}
		taglep_m_max=object.Llep_m.size();
		for(int i=0;i<taglep_m_max;i++){
			object.Lsignal_lep_m.push_back(object.Llep_m[i]);
		}
		taglep_p_max=object.Llep_p.size();
		for(int i=0;i<taglep_p_max;i++){
			object.Lsignal_lep_p.push_back(object.Llep_p[i]);
		}
	}
	if(para.lep.elec.SWITCH==true){
		if(para.flow.exclusive){
			taglep_max=para.signal.NUM_elec;
		}
		else{
			taglep_max=object.Lelec.size();
		}
		for(int i=0;i<taglep_max;i++){
			object.Lsignal_lep.push_back(object.Lelec[i]);
		}
		taglep_m_max=object.Lelec_m.size();
		for(int i=0;i<taglep_m_max;i++){
			object.Lsignal_lep_m.push_back(object.Lelec_m[i]);
		}
		taglep_p_max=object.Lelec_p.size();
		for(int i=0;i<taglep_p_max;i++){
			object.Lsignal_lep_p.push_back(object.Lelec_p[i]);
		}
	}
	if(para.lep.muon.SWITCH==true){
		if(para.flow.exclusive){
			taglep_max=para.signal.NUM_muon;
		}
		else{
			taglep_max=object.Lmuon.size();
		}
		for(int i=0;i<taglep_max;i++){
			object.Lsignal_lep.push_back(object.Lmuon[i]);
		}
		taglep_m_max=object.Lmuon_m.size();
		for(int i=0;i<taglep_m_max;i++){
			object.Lsignal_lep_m.push_back(object.Lmuon_m[i]);
		}
		taglep_p_max=object.Lmuon_p.size();
		for(int i=0;i<taglep_p_max;i++){
			object.Lsignal_lep_p.push_back(object.Lmuon_p[i]);
		}
	}

	std::sort(object.Lsignal_lep.begin(),object.Lsignal_lep.end(),TLV_compare_Pt);
	std::sort(object.Lsignal_lep_m.begin(),object.Lsignal_lep_m.end(),TLV_compare_Pt);
	std::sort(object.Lsignal_lep_p.begin(),object.Lsignal_lep_p.end(),TLV_compare_Pt);
	return(true);

}
